﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.Company;
using SIMS.Global;

public partial class Company_CreateEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["id"] != null)
            {
                int _CompanyId = 0;
                if (int.TryParse(Request.Params["id"].ToString(), out _CompanyId) && _CompanyId > 0)
                {
                    CompanyProfile _profile = new CompanyProfile();
                    CompanyProfileDB _profileDB = new CompanyProfileDB();
                    _profile = _profileDB.GetProfile(_CompanyId);

                    if (_profile.Id > 0)
                    {
                        if (_profile.Error.ErrorNo > 0)
                        {
                            lblMessage.Text = _profile.Error.ErrorMessage;
                        }
                        else
                        {
                            hidCompanyId.Value = _CompanyId.ToString();
                            txtCompanyName.Text = _profile.CompanyName;
                            txtCompanyName2.Text = _profile.CompanyName2;
                            txtContactLine1.Text = _profile.ContactNoLine1;
                            txtContactLine2.Text = _profile.ContactNoLine2;
                            txtAddress1.Text = _profile.AddressLine1;
                            txtAddress2.Text = _profile.AddressLine2;
                            txtFaxNo1.Text = _profile.FaxNoLine1;
                            txtFaxNo2.Text = _profile.FaxNoLine2;
                            txtEmail1.Text = _profile.EmailAddress1;
                            txtEmail2.Text = _profile.EmailAddress2;
                            txtFounderName.Text = _profile.FounderName;
                            txtPrincipalName.Text = _profile.PrincipalName;
                            txtDescription1.Text = _profile.Description1;
                            txtDescription2.Text = _profile.Description2;
                            if (_profile.OpeningYear > 0)
                            {
                                txtOpeningYear.Text = _profile.OpeningYear.ToString();
                            }
                        }
                    }
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                CompanyProfile aCompany = new CompanyProfile();
                CompanyProfileDB _profileDB = new CompanyProfileDB();
                int _CompanyId = 0;

                if (int.TryParse(hidCompanyId.Value, out _CompanyId) && _CompanyId > 0)
                {
                    aCompany = _profileDB.GetProfile(_CompanyId);
                }
                else
                {
                    aCompany.Id = 0;
                    aCompany.Error = new MyError();
                }

                aCompany.CompanyName = txtCompanyName.Text;

                if (txtCompanyName2.Text.Trim().Length > 0)
                {
                    aCompany.CompanyName2 = txtCompanyName2.Text;
                }
                else
                {
                    aCompany.CompanyName2 = string.Empty;
                }

                if (txtAddress1.Text.Trim().Length > 0)
                {
                    aCompany.AddressLine1 = txtAddress1.Text;
                }
                else
                {
                    aCompany.AddressLine1 = string.Empty;
                }

                if (txtAddress2.Text.Trim().Length > 0)
                {
                    aCompany.AddressLine2 = txtAddress2.Text;
                }
                else
                {
                    aCompany.AddressLine2 = string.Empty;
                }

                if (txtContactLine1.Text.Trim().Length > 0)
                {
                    aCompany.ContactNoLine1 = txtContactLine1.Text;
                }
                else
                {
                    aCompany.ContactNoLine1 = string.Empty;
                }

                if (txtContactLine2.Text.Trim().Length > 0)
                {
                    aCompany.ContactNoLine2 = txtContactLine2.Text;
                }
                else
                {
                    aCompany.ContactNoLine2 = string.Empty;
                }

                if (txtFaxNo1.Text.Trim().Length > 0)
                {
                    aCompany.FaxNoLine1 = txtFaxNo1.Text;
                }
                else
                {
                    aCompany.FaxNoLine1 = string.Empty;
                }

                if (txtFaxNo2.Text.Trim().Length > 0)
                {
                    aCompany.FaxNoLine2 = txtFaxNo2.Text;
                }
                else
                {
                    aCompany.FaxNoLine2 = string.Empty;
                }

                if (txtEmail1.Text.Trim().Length > 0)
                {
                    aCompany.EmailAddress1 = txtEmail1.Text;
                }
                else
                {
                    aCompany.EmailAddress1 = string.Empty;
                }

                if (txtEmail2.Text.Trim().Length > 0)
                {
                    aCompany.EmailAddress2 = txtEmail2.Text;
                }
                else
                {
                    aCompany.EmailAddress2 = string.Empty;
                }

                if (txtFounderName.Text.Trim().Length > 0)
                {
                    aCompany.FounderName = txtFounderName.Text;
                }
                else
                {
                    aCompany.FounderName = string.Empty;
                }

                if (txtPrincipalName.Text.Trim().Length > 0)
                {
                    aCompany.PrincipalName = txtPrincipalName.Text;
                }
                else
                {
                    aCompany.PrincipalName = string.Empty;
                }


                if (txtOpeningYear.Text.Trim().Length > 0)
                {
                    Int16 _openingYear = 0;

                    if (Int16.TryParse(txtOpeningYear.Text.Trim(), out _openingYear))
                    {
                        aCompany.OpeningYear = _openingYear;
                    }
                }
                else
                {
                    aCompany.OpeningYear = 0;
                }

                if (txtDescription1.Text.Trim().Length > 0)
                {
                    aCompany.Description1 = txtDescription1.Text;
                }
                else
                {
                    aCompany.Description1 = string.Empty;
                }

                if (txtDescription2.Text.Trim().Length > 0)
                {
                    aCompany.Description2 = txtDescription2.Text;
                }
                else
                {
                    aCompany.Description2 = string.Empty;
                }

                aCompany.IsActive = false;

                _profileDB.SaveProfile(aCompany);

                if (aCompany.Error.ErrorNo > 0)
                {
                    if (aCompany.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = aCompany.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + aCompany.Error.ErrorLocation + ")";
                        lblMessage.Text += aCompany.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + aCompany.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Cache.Remove(Global.CompanyListCacheId);
                    Session[Global.CompanyId] = aCompany.Id.ToString();
                    Session[Global.CompanyName] = aCompany.CompanyName;
                    Response.Redirect("default.aspx");
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

}